package codeine.servlets;

import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import codeine.configuration.ConfigurationManager;
import codeine.jsons.labels.LabelJsonProvider;
import codeine.jsons.project_status.PeersProjectsStatus;
import codeine.servlet.AbstractServlet;
import codeine.servlet.HtmlHeaderWriter;
import codeine.version.VersionItem;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

public class AggregateNodesServlet extends AbstractServlet {
	private static final Logger log = Logger.getLogger(AggregateNodesServlet.class);
	private static final long serialVersionUID = 1L;

	@Inject
	private ConfigurationManager configurationManager;
	@Inject
	private PeersProjectsStatus peersProjectsStatus;
	@Inject
	private NodeAggregator aggregator;
	@Inject private HtmlHeaderWriter htmlHeaderWriter;
	@Inject	private LabelJsonProvider versionLabelJsonProvider;
	
	@Override
	protected void myGet(HttpServletRequest request, HttpServletResponse response) {
		log.debug("aggregate request");
		String projectName = request.getParameter("project");
		PrintWriter writer = getWriter(response);
		htmlHeaderWriter.writeHeader(request, response, projectName, projectName + " / aggregate");
		Comparator<VersionItem> comparator = new Comparator<VersionItem>() {
			@Override
			public int compare(VersionItem o1, VersionItem o2) {
				return o1.version().compareTo(o2.version());
			}
		};
		List<VersionItem> values = Lists.newArrayList(aggregator.aggregate(peersProjectsStatus.peer_to_projects,
				configurationManager.getProjectForName(projectName)).values());
		Collections.sort(values, comparator);
		for (VersionItem item : values) {
			// start building monitored instance line:
			String line = "            <alert><div class=\"alertbar\"><ul>";
			String version = item.version();
			String className = "g";
			String versionClass = "version" + className;
			String versionLabel = versionLabelJsonProvider.labelForVersion(version, projectName);
			if (null != version) {
				line += "<li><a class=\"" + versionClass + "\" href=\"none\">" + versionLabel + "</a></li>";
				line += "<li><a class=\"" + versionClass + "\" href=\"none\">" + item.count() + "</a></li>";
			}
			String projectParams = "&project=" + projectName;
			line += 
					"<li>" + "<input title='number of nodes you would like to see' class=\"version\" type=\"text\" id=\"" + version + "_input\" value=\"" + item.count() + "\"></input>" + "</li>" + 
					"<li>" + "<input title='regular expression on node name' class=\"version\" type=\"text\" id=\"" + version + "_regexp_input\" value=\".*\"></input>" + "</li>" +
					"<li> <button class=\"command\" onClick=\"viewDashboard('" + version + "', '" + projectParams + "', " + false + ")\">View</button> </li>" +  
					"<li> <button class=\"command\" onClick=\"viewDashboard('" + version + "', '" + projectParams + "', " + true + ")\">View-Alerts</button> </li>";
			line += "</ul><br style=\"clear:left\"/></div></alert>";
			writer.println(line);
		}
		htmlHeaderWriter.writeFooter(response);
	}

}
